<?php
// +----------------------------------------------------------------------
// | Author: July
// +----------------------------------------------------------------------
// | 创建时间: 2023-12-27 20:39:42
// +----------------------------------------------------------------------
// | 文件上传控制器
// +----------------------------------------------------------------------


namespace july\julyadmin\admin\controller;

class Upload extends Common
{
    //不需要登录验证的控制器和方法，如：['index']
    protected $noNeedLogin = [];
    //不需要权限验证的控制器和方法，如：['index']
    protected $noPermissionAuth = ['*'];
    //驱动id
    protected $drive_id = '';
    //上传驱动
    protected $upload;

    public function __construct()
    {
        //重新初始化父类
        parent::initialize();

        $this->drive_id = \july\julyadmin\common\model\Config::config('ImageUpload.uploadDrive');
        if (!$this->drive_id) {
            $this->error('没有设置上传驱动');
        }

        $res = \july\julyadmin\uploaddrives\Drives::getDriveNamesPace($this->drive_id);
        if ($res['code'] == 0) {
            $this->error('驱动' . $this->drive_id . '不存在');
        }

        $this->upload = new $res['data']['namespace'];
    }

    /**
     * 图片上传
     * @Author   July
     * @DateTime 2024-01-13T19:40:34+0800
     * @return json
     */
    public function imageUpload()
    {
        $result = $this->upload->imageUpload();
        return json($result);
    }

    /**
     * 文件上传
     * @Author   July
     * @DateTime 2024-01-13T19:40:34+0800
     * @return json
     */
    public function fileUpload()
    {
        $result = $this->upload->fileUpload();
        return json($result);
    }

    /**
     * 检查分片
     * @Author   July
     * @DateTime 2024-01-13T19:40:34+0800
     * @param  string $md5 文件md5
     * @return json
     */
    public function checkChunks($md5)
    {
        $result = $this->upload->checkChunks($md5);
        return json($result);
    }
}
